/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package icontrole.core;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import javax.print.attribute.standard.Severity;

/**
 *
 * @author Amon
 */
public class Export extends XStream {

    private ServerQuanser _server;

    public ServerQuanser getServer() {
        return _server;
    }

    public void setServer(ServerQuanser _server) {
        this._server = _server;
    }

    public Export(ServerQuanser server) {
        super(new DomDriver());
        this._server = server;
    }

    public Export() {
        super(new DomDriver());
        this._server = new ServerQuanser();

    }

    public void Load() {
        this._server.setName("icontrol.xml");
        this._server.setPath(System.getProperty("user.dir"));
        this._server.setPath_name(this._server.getPath() + "\\" + this._server.getName());
        String config = this.getFile(this._server.getPath_name());
        if (!"".equals(config)) {
            this.alias("ServerQuanser", ServerQuanser.class);
            this._server = (ServerQuanser) this.fromXML(config);
        } else {
            this.setFile(this._server.getPath_name(), new ServerQuanser());
        }
    }

    public void Save() {
        ServerQuanser serv = newInstance();
        this.setFile(this._server.getPath_name(), serv);
    }

    private String getFile(String _Path_Name) {
        File file = new File(_Path_Name);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        StringBuffer buffer = null;

        try {
            buffer = new StringBuffer();
            fis = new FileInputStream(file);
            // atribuindo uma leitura mais rapida
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);

            //dis retorna 0 se ano tem mais linhas
            while (dis.available() != 0) {
                //ler a linha e coloca na string
                buffer.append(dis.readLine()).append("\n");
            }

            // liberando todos os recursos
            fis.close();
            bis.close();
            dis.close();

        } catch (FileNotFoundException e) {
            buffer.append("");
        } catch (IOException e) {
            buffer.append("");
        }
        return buffer.toString();
    }

    private void setFile(String _Path_Name, ServerQuanser _Atributos) {
        try {
            // Criando o arquivo
            FileWriter fstream = new FileWriter(_Path_Name);
            BufferedWriter out = new BufferedWriter(fstream);
            String toXML = this.toXML(_Atributos);
            out.write(toXML);
            //Fachando o arquivo
            out.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }

    public void Save(String path) {
        ServerQuanser serv = newInstance();
        this.setFile(path, serv);
    }

    public boolean Load(String path) {
        XStream _xstream = new XStream(new DomDriver());
        String config = this.getFile(path);
        boolean value = true;
        try {
            if (!"".equals(config)) {
                _xstream.alias("ServerQuanser", ServerQuanser.class);
                this._server = (ServerQuanser) _xstream.fromXML(config);
            }
        } catch (Exception e) {
            value = false;
        }
        return value;
    }

    private ServerQuanser newInstance() {
        ServerQuanser serv = new ServerQuanser();
        serv.setConstank1(this._server.getConstank1());
        serv.setConstank2(this._server.getConstank2());
        serv.setIp(this._server.getIp());
        serv.setName(this._server.getName());
        serv.setNivel(this._server.getNivel());
        serv.setNivel_tk1(this._server.getNivel_tk1());
        serv.setNivel_tk2(this._server.getNivel_tk2());
        serv.setOperation(this._server.isOperation());
        serv.setPath(this._server.getPath());
        serv.setPath_name(this._server.getPath_name());
        serv.setPort(this._server.getPort());
       
        return serv;
    }
}
